//
//  DepTableView.m
//  HarmonizePub
//
//  Created by rzhy on 2017/1/22.
//  Copyright © 2017年 Zy. All rights reserved.
//

#import "DepTableView.h"
#import "Config.h"
#import <YYModel.h>
#import "Physical.h"
#import "DepTableViewCell.h"
#import "Physical.h"

static NSString *CellIdentifier = @"DepCell";

@interface DepTableView () <UITableViewDataSource, UITableViewDelegate>


@end

@implementation DepTableView

+ (CGFloat)tableViewHeightWithArray:(NSArray *)array {
    CGFloat height = 0;
    for (NSDictionary *dict in array) {
        Physical *p = [Physical yy_modelWithDictionary:dict];
        CGFloat f = [self calculateHeightWithPhysical:p];
        height += f;
    }
    return height;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.delegate = self;
        self.dataSource = self;
        self.bounces = NO;
        self.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        UINib *nib = [UINib nibWithNibName:[DepTableViewCell registerName] bundle:nil];
        [self registerNib:nib forCellReuseIdentifier:CellIdentifier];
    }
    return self;
}

- (void)setDataArray:(NSArray *)dataArray {
    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dataArray.count];
    for (NSDictionary *dict in dataArray) {
        Physical *p = [Physical yy_modelWithDictionary:dict];
        [arrayM addObject:p];
    }
    _dataArray = [arrayM copy];
    CGFloat height = [DepTableView tableViewHeightWithArray:dataArray];
    self.frame = CGRectMake(8.0f, 0, XHScreenWidth - 16.0f, height);
    [self reloadData];
}

#pragma mark - 计算每个Cell的高度
+ (CGFloat)calculateHeightWithPhysical:(Physical *)physical {
    NSString *ksxj = physical.ksxj;
    CGFloat textHeight = [ksxj boundingRectWithSize:CGSizeMake(XHScreenWidth - 161.5f, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.0f]} context:nil].size.height;
    return textHeight + 16.0f;
}

#pragma mark - tableView delegate/dataSource Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Physical *p = _dataArray[indexPath.row];
    return [DepTableView calculateHeightWithPhysical:p];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DepTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    Physical *p = _dataArray[indexPath.row];
    [cell setCellShowDataWithPhysical:p];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    Physical *p = _dataArray[indexPath.row];
    if ([self.tableDelegate respondsToSelector:@selector(tableView:didSelectRowAtPhysical:)]) {
        [self.tableDelegate tableView:self didSelectRowAtPhysical:p];
    }
}


@end
